Ext.onReady(
        function  (){
            htmlTemplate=new Ext.XTemplate("<div>{postfrom}说：</div><br /><div>{content}</div>");
            viewport=new Ext.Viewport({
                layout:"border",
                border:false,
                items:[
                new Ext.BoxComponent({
                    region:"north",
                    el:"north",
                    height:40,
                    margin:10}),

                new Ext.BoxComponent({
                    region:"south",
                    el:"south"}),

                {
                    region:"center"},
                ]
                });

            formPanel = new Ext.FormPanel( {
			url : './addMessage/',//这个地方在本机web2py测试要改成../
			width : 300,
			height : 280,
			labelWidth : 50,
			bodyStyle : 'padding:10px',
			defaultType : 'textfield',
			items : [
                            {
				fieldLabel : '留言人',
				emptyText : '匿名',
                                maxLength: 15,
                                allowBlank:false,
				name : 'postfrom',
			    },
                            new Ext.form.TextArea({
				width : 200,
				height : 150,
				maxLength : 100,
                                allowBlank:false,
				maxLengthText : 'more than 100',
				fieldLabel : '内容',
				emptyText : '这里是留言内容',
				name : 'content',
			    })]
		});
	    formWindow = new Ext.Window({
                        title:'输入留言',
                        closable:false,
                        height:300,
                        width:300,
                        layout:'table',
                        modal:true,
                        resizable:false,
                        items:[this.formPanel],
                        buttons:[{
                                text : '留言',
                                disabled : false,
                                handler : function(){
                                    if (this.formPanel.form.isValid()) {
                                        this.formPanel.form.submit({
                                            url : './addMessage/',
                                            method:'post',
                                            /*这里判断是否成功的条件不是服务器状态，而是服务器是否返回一个json，
                                             * 里面的success属性为true，如果是则成功，反之都是失败*/
                                            success : function(form,action){
                                                //var jsonData = Ext.util.JSON.decode(action.result.data);
                                                var jsonData=action.result.data;
                                                var win=new Ext.Window({
                                                    closable:true,
                                                    height:200,
                                                    width:150,
                                                    resizable:false
                                                });
                                                win.setPosition(jsonData.x,jsonData.y);
                                                win.show();
                                                htmlTemplate.overwrite(win.body,jsonData);
                                            },
                                            failure : function(result, request) {
                                                Ext.Msg.alert('警告','提交失败！');
                                            },
                                            waitMsg : '正在提交数据...'
                                            });
                                            formWindow.hide();
                                        } else {
                                            Ext.Msg.alert('警告', '没有填写数据。请填写后再提交。');
                                        }
                                    },
                                scope:this    
                                }, {
                                    text : '取消',
                                    handler : function() {
                                    formWindow.hide();
                                },
                                scope:this
                            }]

                    });

        Ext.get("addMessage").on('click',function(){
                        this.formPanel.form.reset();
                        this.formWindow.show();
                    },this);

        function loadAllMessage(){
            Ext.Ajax.request({
                url : './getAllMessage/' , //这个地方在本机测试要改成../
                method: 'GET',
                success: function ( result, request ) {
                    //Ext.Msg.alert('aaa',result.responseText);
                    messages = Ext.util.JSON.decode(result.responseText);
                    messages = messages.data;
                    for (var i=0,len=messages.length;i<len;i++){
                        jsonData=messages[i];
                        makeWindow(jsonData);
                    }
                },
                failure: function ( result, request) { 
                    Ext.Msg.alert('Failed', 'Successfully posted form: '+action.date); 
                },
                scope:this
            });
        }

        function loadLatestMessage(){
                    }
        function makeWindow(jsonData){
           var win=new Ext.Window({
                        closable:true,
                        height:200,
                        width:150,
                        resizable:false
                    });
            win.setPosition(jsonData.x,jsonData.y);
            win.show();
            htmlTemplate.overwrite(win.body,jsonData);
        }
             

        loadAllMessage();
        }
        );
